package 全排列;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 全排列
 * @date 2022/11/19 11:11
 */
public class Demo46 {
    List<List<Integer>> res=new ArrayList<>();
    LinkedList<Integer> path=new LinkedList<>();
    public List<List<Integer>> permute(int[] nums) {
        backTrack(nums,0);
        return  res;
    }

    private void backTrack(int[] nums, int startIndex) {
        //如果长度相等了，则直接添加到结果集
        if(path.size()==nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            //如果path已经含有了则直接跳过
            if(path.contains(nums[i])){
                continue;
            }
            path.add(nums[i]);
            backTrack(nums,0);
            path.removeLast();
        }
    }
}
